package com.pj.wisdomlogistics.ui.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.pj.spinnerlib.views.DensityUtil;
import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.CarPoritionInfo;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.activity.CarPlanActivity;
import com.pj.wisdomlogistics.ui.adapter.CarChooseAdapter;
import com.pj.wisdomlogistics.ui.view.GridSpacingItemDecoration;
import com.pj.wisdomlogistics.ui.view.recyclerview.BaseViewHolder;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;


public class CarChooseFragment extends BaseFragment implements CarChooseAdapter.onItemClick {
    @BindView(R.id.rv_choose_car)
    RecyclerView mRvChooseCar;

    private CarChooseAdapter mAdapter;
    private List<CarPoritionInfo.CarListBean> mList;

    private int mSelectedPos = -1;

    private Drawable drawable_blue = null;
    private Drawable drawable_white = null;

    @Override
    protected void initView() {
        mRvChooseCar.setLayoutManager(new GridLayoutManager(getContext() , 2 , LinearLayoutManager.VERTICAL , false));
        mRvChooseCar.addItemDecoration(new GridSpacingItemDecoration(2 , DensityUtil.dip2px(getContext() , 20) , false));
        mAdapter = new CarChooseAdapter(getContext());
        mRvChooseCar.setAdapter(mAdapter);
        mAdapter.setListener(this);

        drawable_blue= getResources().getDrawable(R.drawable.car_blue);
        drawable_white= getResources().getDrawable(R.drawable.car_white);
        drawable_blue.setBounds(0, 0, drawable_blue.getMinimumWidth(), drawable_blue.getMinimumHeight());
        drawable_white.setBounds(0, 0, drawable_white.getMinimumWidth(), drawable_white.getMinimumHeight());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    public int getContentView() {
        return R.layout.fragment_choose_car;
    }

    @Override
    protected boolean isApplyEventBus() {
        return true;
    }

    @Override
    protected boolean isApplyButterKnife() {
        return true;
    }

    @Override
    protected void onEventCome(EventCenter eventCenter) {
        if(eventCenter.getEventCode() == Cons.CarInfo.CAR_CHOOSE){
            mList = (List<CarPoritionInfo.CarListBean>) eventCenter.getData();
            mAdapter.setList(mList);
        }
    }

    @Override
    public void itemClick(int position , int carId) {
        EventBus.getDefault().post(new EventCenter<>(Cons.CarInfo.CAR_CHOOSE_OK , position));

        BaseViewHolder vh;
        if(mSelectedPos == -1){
            vh = (BaseViewHolder) mRvChooseCar.findViewHolderForLayoutPosition(position);
        }else {
            vh = (BaseViewHolder) mRvChooseCar.findViewHolderForLayoutPosition(mSelectedPos);
        }

        if(vh != null){
            vh.getView(R.id.rel_choose_car).setBackgroundResource(R.drawable.select_car_2);
            vh.getTextView(R.id.tv_car_model).setCompoundDrawables(drawable_white,null,null,null);
        }else{
            mAdapter.notifyItemChanged(mSelectedPos);
        }

        if(mSelectedPos == -1){
            mSelectedPos = position;
            mList.get(mSelectedPos).setSelected(true);
        }else{
            mList.get(mSelectedPos).setSelected(false);
            mList.get(mSelectedPos).setSelected(false);
            mSelectedPos = position;
            mList.get(mSelectedPos).setSelected(true);
            vh.getView(R.id.rel_choose_car).setBackgroundResource(R.drawable.select_car);
            vh.getTextView(R.id.tv_car_model).setCompoundDrawables(drawable_blue,null,null,null);
            vh = (BaseViewHolder) mRvChooseCar.findViewHolderForLayoutPosition(mSelectedPos);
            vh.getView(R.id.rel_choose_car).setBackgroundResource(R.drawable.select_car_2);
            vh.getTextView(R.id.tv_car_model).setCompoundDrawables(drawable_white,null,null,null);
        }

        Intent mIntent = new Intent(getContext() , CarPlanActivity.class);
        mIntent.putExtra(Cons.CarInfo.CAR_PLAN , carId);
        startActivity(mIntent);

    }
}
